﻿using System;
using System.Windows.Input;


namespace MVVMS
{
    /// <summary>
    /// Typed Implementation of the ICommand Interface
    /// Use this in your ViewModels to expose commands to
    /// your view.
    /// </summary>
    public class ViewModelCommand<T> : ICommand
    {
        private readonly Action<T> execute;
        private readonly Func<T, bool> canExecute;

        public ViewModelCommand(Action<T> execute, Func<T, bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public ViewModelCommand(Action<T> execute)
        {
            this.execute = execute;
            this.canExecute = (parameter) => { return true; };
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute((T)parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, EventArgs.Empty);
        }

        public void Execute(object parameter)
        {
            execute((T) parameter);
        }
    }
}
